﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmbeddedSharp.Core;
using System.Configuration;
using System.Windows.Forms.DataVisualization.Charting;
using EmbeddedSharp.Scripting;
using System.IO;
using System.Drawing.Imaging;

namespace EmbeddedSharp.Scripting.Visualization
{
    public partial class PlotView : Form, IVisualizationProvider
    {
        public ScriptingObject script = null;

        private StringBuilder source = new StringBuilder();

        public PlotView()
        {
            InitializeComponent();

            script = new ScriptingObject();
            VisualizationContextFuncNode.Provider = this;
        }

        public PlotView(string fileName) : base()
        {
            source = new StringBuilder(File.ReadAllText(fileName));
            this.script = new ScriptingObject(source.ToString());
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region IVisualizationProvider Members

        public void AddLine(IUnaryParameterFunction func)
        {
            throw new NotImplementedException();
        }

        public void AddLine(IUnaryParameterFunction func, double from, double to)
        {
            PlotSharpSeries series = new PlotSharpSeries("", func, from, to);
            series.ChartType = SeriesChartType.Line;

            this.chart1.Series.Add(series);
        }

        public void AddCurve(IUnaryParameterFunction func)
        {
            throw new NotImplementedException();
        }

        public void AddCurve(
            string name, IUnaryParameterFunction func, double from, double to)
        {
            PlotSharpSeries series = new PlotSharpSeries(name, func, from, to);
            series.ChartType = SeriesChartType.Spline;

            this.chart1.Series.Add(series);
        }

        public void AddCurves(IUnaryParameterFunction[] functions)
        {
            foreach (IUnaryParameterFunction func in functions)
            {
                AddCurve(func);
            }
        }

        public void AddCurves(IUnaryParameterFunction[] functions, double from, double to)
        {
            foreach (IUnaryParameterFunction f in functions)
            {
                AddCurve("", f, from, to);
            }
        }

        public void AddPoint(Point2D vertex)
        {
            throw new NotImplementedException();
        }

        public void AddLine(Point2D s, Point2D e)
        {
            throw new NotImplementedException();
        }

        public void SaveImage(string fileName)
        {
            this.chart1.SaveImage(fileName, ChartImageFormat.Png);
        }

        #endregion

        private void cmdRun_Click(object sender, EventArgs e)
        {
            object ret = script.Evaluate(this.tbCmd.Text);
            this.tbCmd.Items.Add(this.tbCmd.Text);
            this.source.Append(this.tbCmd.Text + ";\n\r");

            if (ret != null)
            {
                this.statusLabel.Text = ret.ToString();
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog d = new OpenFileDialog();

            if (d.ShowDialog() == DialogResult.OK)
            {
                UpdateTitle(d.FileName);
            }
        }

        private string fileName = string.Empty;

        public void UpdateTitle(string fileName)
        {
            this.Text = "PlotSharp - [" + fileName + "]";

            this.fileName = fileName;
            source = new StringBuilder(File.ReadAllText(fileName));
            this.script = new ScriptingObject(source.ToString());

            this.script.Run();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
                File.WriteAllText(fileName, source.ToString());
            }
            else
            {
                SaveAs();
            }
        }

        public void SaveAs()
        {
            SaveFileDialog d = new SaveFileDialog();

            d.Filter = "Embedded# Script File |*.ess";
            if (d.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(d.FileName, source.ToString());
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SaveFileDialog d = new SaveFileDialog();

            d.Filter = "PNG File|*.png";
            if (d.ShowDialog() == DialogResult.OK)
            {
                chart1.SaveImage(d.FileName, ImageFormat.Png);
            }
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            
        }
    }
}
